<template>
    <div>
        <n-modal v-if="props.showModal" v-model:show="props.showModal" :mask-closable="true" @update:show="$emit('CheckShowModal', false)">
            <n-card style="width: 600px" title="添加菜单" :bordered="false" size="huge" role="dialog" aria-modal="true">
                <template #header-extra>
                    <n-button type="error" @click="$emit('CheckShowModal', false)">
                        X
                    </n-button>
                </template>
                <n-form ref="formRef" :model="model" :rules="rules">
                    <n-form-item path="uName" label="菜单名">
                        <n-input v-model:value="model.nName" placeholder="请输入菜单名" />
                    </n-form-item>
                    <n-form-item path="nLevell" label="等级">
                        <n-input v-model:value="model.nLevel" placeholder="请输入等级"/>
                    </n-form-item>
                    <n-form-item path="URL" label="URL">
                        <n-input v-model:value="model.nUrl"  placeholder="请输入URL" />
                    </n-form-item>
                    <n-row :gutter="[0, 24]">
                        <n-col :span="24">
                            <div style="display: flex; justify-content:center">
                                <n-button round type="primary" @click="UpdateUserFile">
                                    添加
                                </n-button>
                            </div>
                        </n-col>
                    </n-row>
                </n-form>
            </n-card>
        </n-modal>

        <n-skeleton v-else text :repeat="2" /> <n-skeleton text style="width: 60%" />
    </div>
</template>

<script lang="ts" setup>
import { defineProps, ref, reactive, onMounted } from 'vue';
import {addNav} from "@/api/menu"
const formRef = ref();
const emit = defineEmits(['CheckShowModal','reloadpage'])
const props = defineProps({
    showModal: {
        type: Boolean,
        default: false
    }
})

const model = reactive({
    nName: '',
    nLevel: 1,
    nUrl: '',
})

const rules = {
    nName: [
        {
            required: true,
            message: '请输入姓名',
            trigger: ['input', 'blur']
        }
    ],
    nLevel: [
        {
            required: true,
            message: '请输密码',
            trigger: ['input', 'blur']
        }
    ],
    nUrl: [
        {
            required: true,
            message: '请输入URL',
            trigger: ['input', 'blur']
        },
    ]
}

const UpdateUserFile = (e:Event) => {
    e.preventDefault()
    formRef.value.validate((err:any)=>{
        if(err){
            console.log(err)
        }else{
            addNav(model).then((res:any)=>{
                emit('CheckShowModal',false)
                emit('reloadpage')
            })
        }
    })
}
</script>

<style scoped lang=scss>

</style>